import { _decorator, Animation, Component, Input, instantiate, Label, Node, Prefab, resources, Sprite, UITransform } from 'cc';
import { GLOBALUtil } from './GlobalUtil';
import { OtherPlayerPageManager } from './OtherPlayerPageManager';
import { OtherPlayerInfoManager } from './OtherPlayerInfoManager';
const { ccclass, property } = _decorator;

@ccclass('RankManager')
export class RankManager extends Component {
    @property({type: Node, tooltip: "rankPanel"})
    private rankPanel: Node = null;
    @property({type: Node, tooltip: "ScrollViewRank"})
    private ScrollViewRank: Node = null;

    private itemMap: Map<number, Node>;
    protected async onEnable() {
        //播放动画
        this.rankPanel.getComponent(Animation).play();

        type Data = {
            user_id: number,
            nickname: string,
            avatar_url: string,
            coins: number,
        }
        try {
            this.itemMap = new Map<number, Node>();
            //获取金币排名前50的玩家
            const data = await GLOBALUtil.getCoinTop50() as object[];
            data.forEach((item: Data, index) => {
                resources.load("Prefabs/PlayerRankInfoNode", Prefab, (error, Prefab) => {
                    if (error) {
                        console.error(error);
                        return;
                    }
                    const PlayerRankInfoNode = instantiate(Prefab);
                    const AvatarSP = PlayerRankInfoNode.getChildByPath("Mask0/Avatar").getComponent(Sprite);
                    const NicknameLB = PlayerRankInfoNode.getChildByName("Nickname").getComponent(Label);
                    const UserIDLB = PlayerRankInfoNode.getChildByName("UserID").getComponent(Label);
                    const CoinNumLB = PlayerRankInfoNode.getChildByPath("img_coin/coinNum").getComponent(Label);
                    const RankNumLB = PlayerRankInfoNode.getChildByName("RankNum").getComponent(Label);

                    GLOBALUtil.loadNetImage(item.avatar_url, (SpriteFrame) => {
                        AvatarSP.spriteFrame = SpriteFrame;
                    })
                    NicknameLB.string = item.nickname;
                    UserIDLB.string = `ID:${item.user_id}`;
                    CoinNumLB.string = `$${item.coins}`;
                    RankNumLB.string = `No.${index + 1}`;
                    
                    if (index == 0) {
                        PlayerRankInfoNode.setPosition(-125, 255, 0);
                    } else {
                        PlayerRankInfoNode.setPosition(-125, -index * 115 + 47, 0);
                    }

                    const Content = this.ScrollViewRank.getChildByPath("view/content");

                    //为头像添加点击打开个人面板的功能
                    const avatar = PlayerRankInfoNode.getChildByPath("Mask0/Avatar");
                    avatar.on(Input.EventType.TOUCH_END, () => {
                        this.openPersonalPanel(item.user_id);
                    }, this);

                    this.itemMap.set(item.user_id, PlayerRankInfoNode);
                    if (index == 0) {
                        this.rankPanel.addChild(PlayerRankInfoNode);
                    } else {
                        Content.addChild(PlayerRankInfoNode);
                        const Height = Content.children.length * 115;
                        Content.getComponent(UITransform).setContentSize(410, Height);
                    }
                })
            })
        } catch (error) {
            console.error(error)
        }
    }

    protected onDisable(): void {
        this.itemMap.forEach((item, key) => {
            item.destroy();
        });
    }

    //点击排行榜的头像，打开他人面板
    async openPersonalPanel(user_id: number) {
        type Data = {
            user_id: number,
            nickname: string,
            avatar_url: string,
            card_frame: JSON,
            designation: JSON,
            LastLoginTime: string
        }
        const response = await GLOBALUtil.getUserInfoByUserID(user_id) as any;
        resources.load("Prefabs/PersonalNode", Prefab, (error, prefab) => {
            if (error) {
                console.error(error);
                return;
            }
            const PersonalNode = instantiate(prefab);
            const data: Data = {
                user_id: response.user_id,
                nickname: response.nickname,
                avatar_url: response.avatar_url,
                card_frame: JSON.parse(response.card_frame),
                designation: JSON.parse(response.designation),
                LastLoginTime: response.last_login_time,
            }
            PersonalNode.getComponent(OtherPlayerPageManager).setOtherPlayerPageData(data);
            PersonalNode.getChildByPath('PersonalPanel/InfoNode').getComponent(OtherPlayerInfoManager).setUserInfo(data);
            PersonalNode.parent = this.node;
        });
    }

    onClickClose() {
        this.node.active = false;
    }
}


